package jbase.project.system.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;

import jbase.project.system.exception.SysException;

/**
 * 文件操作助手
 * 
 * @author leecheng
 * 
 */
public class FileUtils {

	public static byte[] getBytes(File file) {
		try {
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			FileInputStream fis = new FileInputStream(file);
			int readCount = 0;
			byte[] buff = new byte[1024];
			while ((readCount = fis.read(buff)) != -1) {
				baos.write(buff, 0, readCount);
			}
			fis.close();
			return baos.toByteArray();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static OutputStream createOutput(File file) throws SysException {
		try {
			return new FileOutputStream(file);
		} catch (FileNotFoundException e) {
			throw new SysException(e);
		}
	}

	public static OutputStream createOutput(String file) throws SysException {
		return createOutput(new File(file));
	}

	public static String readText(File file, String encoding) {
		try {
			return new String(getBytes(file), encoding);
		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException(e);
		}
	}

}
